/*



Протокол:
255 N K
N - двоичное число вида 0000xxxx, где xxxx - биты включения огней (Red, Yellow, Green, Blue).
K - двоичное число вида aaaabbbb, где aaaa - уровень громкости левого канала, bbbb - правого (от 0 до 12).
*/

#include <FastLED.h>

//Red Yellow Green Blue
// Номера контактов - выходов на огни
const byte CHAN_PIN[] = {2, 3, 4, 5};
// Текущее состояние сигналов
bool state[] = {false, false, false, false};

byte speedFade = 150;

#define  DATA_PIN     6
#define  NUM_LEDS     24

CRGB leds[NUM_LEDS];

unsigned long lastRXTime = 0;

void fadeall() 
{ 
  for(int i = 0; i < NUM_LEDS; i++) 
  { 
    leds[i].nscale8(200); 
  } 
}

void setup()
{
  Serial.begin(115200);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  for (int i = 0; i < 4; i++)
  {
    state[i] = false;
  }

}

void loop()
{
  while (Serial.available() > 0) 
  {
    byte v = Serial.read();

    for (int i = 3; i >= 0; i--)
    {
      if ((v >> i) & 1)
        state[i] = true;
      else
        state[i] = false;
    }

    if (state[0])
    {
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[12+0] = CRGB::Red;
      leds[12+1] = CRGB::Red;
      leds[12+2] = CRGB::Red;
      digitalWrite(2, HIGH);
    }
    else
    {
      leds[0].nscale8(speedFade);
      leds[1].nscale8(speedFade);
      leds[2].nscale8(speedFade);
      leds[12+0].nscale8(speedFade);
      leds[12+1].nscale8(speedFade);
      leds[12+2].nscale8(speedFade);
      digitalWrite(2, LOW);
    }
    
    if (state[1])
    {
      leds[3] = CRGB::Yellow;
      leds[4] = CRGB::Yellow;
      leds[5] = CRGB::Yellow;
      leds[12+3] = CRGB::Yellow;
      leds[12+4] = CRGB::Yellow;
      leds[12+5] = CRGB::Yellow;
      digitalWrite(3, HIGH);
    }
    else
    {
      leds[3].nscale8(speedFade);
      leds[4].nscale8(speedFade);
      leds[5].nscale8(speedFade);
      leds[12+3].nscale8(speedFade);
      leds[12+4].nscale8(speedFade);
      leds[12+5].nscale8(speedFade);
      digitalWrite(3, LOW);
    }
    
    if (state[2])
    {
      leds[6] = CRGB::Green;
      leds[7] = CRGB::Green;
      leds[8] = CRGB::Green;
      leds[12+6] = CRGB::Green;
      leds[12+7] = CRGB::Green;
      leds[12+8] = CRGB::Green;
      digitalWrite(4, HIGH);
    }
    else
    {
      leds[6].nscale8(speedFade);
      leds[7].nscale8(speedFade);
      leds[8].nscale8(speedFade);
      leds[12+6].nscale8(speedFade);
      leds[12+7].nscale8(speedFade);
      leds[12+8].nscale8(speedFade);
      digitalWrite(4, LOW);
    }
    
    if (state[3])
    {
      leds[9] = CRGB::Blue;
      leds[10] = CRGB::Blue;
      leds[11] = CRGB::Blue;
      leds[12+9] = CRGB::Blue;
      leds[12+10] = CRGB::Blue;
      leds[12+11] = CRGB::Blue;
      digitalWrite(5, HIGH);
    }
    else
    {
      leds[9].nscale8(speedFade);
      leds[10].nscale8(speedFade);
      leds[11].nscale8(speedFade);
      leds[12+9].nscale8(speedFade);
      leds[12+10].nscale8(speedFade);
      leds[12+11].nscale8(speedFade);
      digitalWrite(5, LOW);
    }

    lastRXTime = millis();
  }

  if (millis() - lastRXTime > 3000)
  {
    for (int i = 0; i < 4; i++)
    {
      state[i] = false;
    }
  }


  FastLED.show();

  

  /*static unsigned long fadeTime = millis();
  if (millis() - fadeTime >= 10)
  {
    fadeall();
    fadeTime = millis();
  }*/
}
